﻿using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;

namespace TeBigScreenWpf.Pages
{
    /// <summary>
    /// Interaction logic for Page14.xaml
    /// </summary>
    public partial class Page14 : Page
    {
        private IntPtr WindowHandle = IntPtr.Zero;
        private Window Window = null;

        public Page14()
        {
            InitializeComponent();

            Loaded += Page_Loaded;
            Unloaded += Page14_Unloaded;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            Window = Window.GetWindow(this);

            WindowHandle = new WindowInteropHelper(Window).Handle;

            ComponentDispatcher.ThreadFilterMessage += ComponentDispatcher_ThreadFilterMessage;

            ui_web.Navigate("http://10.224.226.230/map/map1.html");
        }

        private void Page14_Unloaded(object sender, RoutedEventArgs e)
        {
            ComponentDispatcher.ThreadFilterMessage -= ComponentDispatcher_ThreadFilterMessage;

            ui_web.Navigate("abount:blank");
        }

        private void ComponentDispatcher_ThreadFilterMessage(ref MSG m, ref bool handled)
        {
            if (m.message >= 0x0204 && m.message <= 0x0206 && IsChildWindowOf(m.hwnd, this.WindowHandle))
            {
                switch (m.message)
                {
                    case Win32.WM_RBUTTONDOWN:
                        break;

                    case Win32.WM_RBUTTONUP:
                        {
                            MouseDevice mouseDev = InputManager.Current.PrimaryMouseDevice;
                            MouseButtonEventArgs mouseEvent = new MouseButtonEventArgs(mouseDev, 0, MouseButton.Right)
                            {
                                RoutedEvent = PreviewMouseRightButtonUpEvent
                            };

                            Window.RaiseEvent(mouseEvent);
                        }
                        break;

                    case Win32.WM_RBUTTONDBLCLK:
                        break;
                }

                //SendMessage(this.WindowHandle, m.message, m.wParam, m.lParam);

                handled = true;
            }
        }

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern bool IsChild(IntPtr hWndParent, IntPtr hwnd);

        public bool IsChildWindowOf(IntPtr child, IntPtr parent)
        {
            return child.ToInt32() == parent.ToInt32() || IsChild(parent, child);
        }
    }
}